<?php 

defined( '_JEXEC' ) or die( 'Restricted Access' );
jimport( 'joomla.application.component.view' );

class ShopViewNew_Good extends JView
{
	function display( $tmpl = null )
    {
		$model = $this->getModel();
		$id = JRequest::getVar('id', JRequest::getVar('cid'));
		$editor = & JFactory::getEditor();
		$this->assignRef("editor",$editor);
		
        if (is_array($id)) {
            $id = $id[0];
        }
		
		JToolBarHelper::title(JText::_('Добавить товар'),'generic.png');
		if($id>0)
			JToolBarHelper::apply('applyGood');
        JToolBarHelper::save('saveGood');
		JToolBarHelper::cancel('cancelGood');
		
		$db = &JFactory::getDBO();
		$query = "SELECT name,id FROM #__shop_categories";
		$db->setQuery($query);
		$categories = $db->loadObjectList();
			
		
		$this->assignRef('categories',$categories);
		$query = "SELECT * FROM #__shop_fields WHERE system=1 AND all_cats=1 AND published=1";// выбираем все поля системные поля и поля для всех категорий
		$db->setQuery($query);
		$all_fields = $db->loadObjectList();
		$this->assignRef('all_fields',$all_fields);
		
		
		$db->setQuery("SELECT id,name,published FROM #__shop_keywords WHERE published=1 ORDER BY name ASC");
		$keywords = $db->loadObjectList();
		$this->assignRef('keywords',$keywords);
		if($id!=0){
			$model->setId($id);
			$item = $model->getData();
			
			$db->setQuery("SELECT id,name FROM #__shop_keywords WHERE locate('<id>".(int)$id."</id>',products)>0 AND published=1 ORDER BY id ASC");
			$related_keywords = $db->loadObjectList('id');
			$this->assignRef('related_keywords',$related_keywords);
			
			foreach($all_fields as $key=>$field){
				$query = "SELECT * FROM #__shop_fields_values WHERE object_id=".(int)$id." AND field_id=".(int)$field->id;
				$db->setQuery($query);
				$field_values[$key] = $db->loadObjectList(); 
			}
			
			$this->assignRef('field_values',$field_values);
			
		/*	
			$query = "SELECT * FROM #__shop_fields_values WHERE object_id=".$item->id;
			$db->setQuery($query);
			$fields_values = "";
		*/
			$this->assignRef('item', $item);
		}
		else{			
			$query = "SELECT * FROM #__shop_fields WHERE all_cats=1";// выбираем все поля , которые установлены для всех категорий
			$db->setQuery($query);
			$fields = $db->loadObjectList();		
		}
				
		parent::display($tmpl);
		
	}
}
?>